package com.ysh.xiaomi.web.servelt;

import com.ysh.xiaomi.pojo.TbUser;
import com.ysh.xiaomi.service.UserService;
import com.ysh.xiaomi.service.impl.UserServiceImpl;
import com.ysh.xiaomi.utils.StringConstans;
import com.ysh.xiaomi.utils.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 袁胜虎 on 2022/8/21 16:23
 */
@WebServlet("/adminLogin")
public class AdminLoginServlet extends BaseServlet {
    private boolean admin;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.login(resp, req);
    }
    UserService userService =new UserServiceImpl();
    public  String login(HttpServletResponse response, HttpServletRequest request){
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        if (StringUtils.isEmpty(username)){
            request.getSession().setAttribute("exp","用户名字不能为空");
            return StringConstans.FORWARD+"/error/error.jsp";
        }
        if(StringUtils.isEmpty(password)){
            request.getSession().setAttribute("exp","密码不能为空");
            return StringConstans.FORWARD+"/error/error.jsp";
        }

       TbUser admin = userService.adminLogin(username,password);
        if(admin==null){
          request.getSession().setAttribute("exp","用户名或密码错误");
            return StringConstans.FORWARD+"/error/error.jsp";
       }
        request.getSession().setAttribute("admin",username);
        return StringConstans.REDIRECT+"admin/admin.jsp";
    }
}
